/*
* Copyright (C) 2019 ~ 2020 Uniontech Software Technology Co.,Ltd.
*
* Author:     helei <helei@uniontech.com>
*
* Maintainer: helei <helei@uniontech.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "StyleButton.h"

//#include <DApplicationHelper>
#include <QPainterPath>
#include <QStyle>
#include <QStyleOptionButton>
#include <QStylePainter>

constexpr int margins = 10;
constexpr int radius = 8;

StyleButton::StyleButton(const QString text, QWidget* parent)
    : QPushButton(text, parent)
{
    QFont f = font();
    f.setPixelSize(16);
    setFlat(true);
    setContentsMargins(margins, margins, contentsMargins().top(), contentsMargins().bottom());
}

void StyleButton::enterEvent(QEvent* e)
{
    m_isEnter = true;
    QPushButton::enterEvent(e);
}
void StyleButton::leaveEvent(QEvent* e)
{
    m_isEnter = false;
    QPushButton::leaveEvent(e);
}

void StyleButton::paintEvent(QPaintEvent* event)
{
    Q_UNUSED(event)

    QStylePainter stylePainter(this);
    stylePainter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    QRectF rect = this->rect().adjusted(1, 1, -1, -1);
    QPainterPath painterPath;
    painterPath.addRoundedRect(rect, radius, radius);
    QPalette pa = this->palette();
    QColor baseColor = pa.color(QPalette::Button);
    QColor backGroud = baseColor;

    if (m_isEnter)
    {
        backGroud = baseColor.darker(120);
    }

    if (isChecked())
    {
        backGroud = pa.color(QPalette::Highlight);
    }
    else if (isDown())
    {
        backGroud = baseColor.darker(150);
    }

    stylePainter.setPen(QPen(QColor(10, 45, 110)));
    stylePainter.setBrush(backGroud);
    stylePainter.drawPath(painterPath);

    QStyleOptionButton btn;
    initStyleOption(&btn);
    stylePainter.drawControl(QStyle::CE_PushButtonLabel, btn);
}
